// pages/geren/shezhi/shezhi.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isFucha: false, //复查提醒，默认关闭
        time: '06:00',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //切换复查
    qhFucha(e) {

        this.setData({
            isFucha: e.detail.value
        })
        if (!e.detail.value) {
            api.delWxRemind(function (res) {
                api.findWxRemind()
            })
        }
    },

    //时间选择取消
    bindTimecancel: function () {
        var that = this
        that.setData({
            isFucha: false,

        })
    },


    //时间选择确认
    bindTimeChange: function (e) {
        app.log(e)
        var that = this
        app.log('picker发送选择改变，携带值为', e.detail.value)
        that.setData({
            time: e.detail.value

        })

        wx.showModal({
            title: '订阅授权',
            // content: '请勾选底部 总是保持以上选择，不再询问',

            mask: true,
            duration: 3000,
            success: function (res) {
                if (res.confirm) {

                    requestMsg().then(res => {

                        if ('ok' === res) {
                            app.log('最终订阅成功')

                            //TODO: 将订阅时间发给服务器

                            if (app.globalData.wxRemind == null) {
                                //add
                                let wxRemind = {
                                    time: '2000-01-01 ' + that.data.time + ':00'
                                }
                                api.addWxRemind(wxRemind, function (res) {
                                    api.findWxRemind()
                                })

                            } else {
                                //put
                                let wxRemind = {
                                    time: '2000-01-01 ' + that.data.time + ':00',
                                    wxRemindId: app.globalData.wxRemind.wxRemindId,

                                }
                                api.putWxRemind(wxRemind, function (res) {
                                    api.findWxRemind()
                                })
                            }


                        } else {
                            app.log('最终订阅失败')
                            that.setData({
                                isFucha: false,

                            })
                        }
                    })

                } else if (res.cancel) {
                    app.log('用户点击取消')
                    that.setData({
                        isFucha: false,

                    })
                }

                app.log('==确认订阅授权')

            }


        })


    },
    onTabItemTap: function () {
        var that = this
        let rtime = '06:00'
        if (app.globalData.wxRemind != null) {
            rtime = (app.globalData.wxRemind.time).substr(11)
            rtime = rtime.substr(0, 5)
        }
        app.log('====>>rtime\n' + rtime)

        that.setData({
            userInfo: app.globalData.userInfo,
            isC: app.globalData.cels,
            isF: !app.globalData.cels,
            isAudio: app.globalData.beep,
            isFucha: app.globalData.wxRemind == null ? false : true,
            time: rtime,


        })
    }
})